﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;
using ESOL.Business.Fa;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainAssetUseApplication : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Variable
        public FaUsedApplicationEntity SelectedUsedApplication { get; set; }
        #endregion

        #region Construction
        public frmMaintainAssetUseApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FUA", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbUsedType, "UT", false);
        }
        private void SetFields()
        {
            if (SelectedUsedApplication == null)
            {
                return;
            }
            txtApplicationCode.Text = SelectedUsedApplication.ApplicationCode;
            if (string.IsNullOrEmpty(txtApplicationCode.Text))
            {
                txtApplicationCode.Text = "自动生成";
            }
            if (SelectedUsedApplication.ApplicationDate.ToString("yyyy-MM-dd") == "1900-01-01"
                || SelectedUsedApplication.ApplicationDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                if (ThisActionType == ActionType.New)
                {
                    dtpApplicationDate.EditValue = DateTime.Today;
                }
                else
                {
                    dtpApplicationDate.EditValue = null;
                }
            }
            else
            {
                dtpApplicationDate.EditValue = SelectedUsedApplication.ApplicationDate;
            }
            if (ThisActionType == ActionType.New)
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(CommonParameter.CurrentUser.PersonId);
                if (person != null)
                {
                    txtApplicationPerson.Tag = person;
                    txtApplicationPerson.Text = person.NameCn + " (" + person.PersonCode + ")";

                    txtPerson.Tag = person;
                    txtPerson.Text = person.NameCn + " (" + person.PersonCode + ")";

                    MstDepartmentEntity dept = MstDepartmentBs.GetMstDepartmentByID(person.DepartmentId);
                    if (dept != null)
                    {
                        txtDepartment.Tag = dept;
                        txtDepartment.Text = dept.DepartmentName + " (" + dept.DepartmentCode + ")";
                    }
                }
            }
            else
            {
                if (string.IsNullOrEmpty(SelectedUsedApplication.ApplicationPerson) == false)
                {
                    MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectedUsedApplication.ApplicationPerson);
                    if (person != null)
                    {
                        txtApplicationPerson.Tag = person;
                        txtApplicationPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                    }
                }
            }
            txtReason.Text = SelectedUsedApplication.Reason;
            cmbStatus.SetSelectedValue(SelectedUsedApplication.Status);
            cmbUsedType.SetSelectedValue(SelectedUsedApplication.UsedType);
            txtRemark.Text = SelectedUsedApplication.Remark;

            if (string.IsNullOrEmpty(SelectedUsedApplication.AssetId) == false)
            {
                FaAssetsEntity asset = FaAssetsBs.GetByID(SelectedUsedApplication.AssetId);
                if (asset != null)
                {
                    txtAsset.Tag = asset;
                    txtAsset.Text = asset.AssetCode + " (" + asset.AssetName + ")";
                }
            }
            if (string.IsNullOrEmpty(SelectedUsedApplication.DepartmentId) == false)
            {
                MstDepartmentEntity dept = MstDepartmentBs.GetMstDepartmentByID(SelectedUsedApplication.DepartmentId);
                if (dept != null)
                {
                    txtDepartment.Tag = dept;
                    txtDepartment.Text = dept.DepartmentName + " (" + dept.DepartmentCode + ")";
                }
            }
            if (string.IsNullOrEmpty(SelectedUsedApplication.PersonId) == false)
            {
                MstPersonEntity person = MstPersonBs.GetMstPersonByID(SelectedUsedApplication.PersonId);
                if (person != null)
                {
                    txtPerson.Tag = person;
                    txtPerson.Text = person.NameCn + " (" + person.PersonCode + ")";
                }
            }
            if (SelectedUsedApplication.FromDate.ToString("yyyy-MM-dd") == "1900-01-01"
                || SelectedUsedApplication.FromDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpFromDate.EditValue = null;
            }
            else
            {
                dtpFromDate.EditValue = SelectedUsedApplication.FromDate;
            }
            if (SelectedUsedApplication.EndDate.ToString("yyyy-MM-dd") == "1900-01-01"
                || SelectedUsedApplication.EndDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpEndDate.EditValue = null;
            }
            else
            {
                dtpEndDate.EditValue = SelectedUsedApplication.EndDate;
            }

            UICommonFunctions.SetTextBoxDate(txtOperatedDate, SelectedUsedApplication.OperatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtOperatedBy, SelectedUsedApplication.OperatedBy);
            UICommonFunctions.SetTextBoxDate(txtApprovedDate, SelectedUsedApplication.ApprovedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, SelectedUsedApplication.ApprovedPerson);
            UICommonFunctions.SetTextBoxDate(txtConfirmedDate, SelectedUsedApplication.ConfirmedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtConfirmedBy, SelectedUsedApplication.ConfirmedBy);
        }
        private void SetEntity()
        {
            if (SelectedUsedApplication==null)
            {
                SelectedUsedApplication = new FaUsedApplicationEntity();
                SelectedUsedApplication.ApplicationId = Guid.NewGuid().ToString();
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                SelectedUsedApplication.ApplicationDate =Convert.ToDateTime(dtpApplicationDate.EditValue);
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                SelectedUsedApplication.ApplicationPerson = ((MstPersonEntity)txtApplicationPerson.Tag).PersonId;
            }
            SelectedUsedApplication.Reason = txtReason.Text;
            SelectedUsedApplication.Remark = txtRemark.Text;
            if (cmbUsedType.SelectedValue != null && cmbUsedType.SelectedValue.ToString().Trim() != "")
            {
                SelectedUsedApplication.UsedType = cmbUsedType.SelectedValue.ToString();
            }
            SelectedUsedApplication.AssetId = null;
            if (txtAsset.Tag != null && txtAsset.Tag.GetType() == typeof(FaAssetsEntity))
            {
                SelectedUsedApplication.AssetId = ((FaAssetsEntity)txtAsset.Tag).AssetId;
            }
            SelectedUsedApplication.DepartmentId = null;
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                SelectedUsedApplication.DepartmentId = ((MstDepartmentEntity)txtDepartment.Tag).DepartmentId;
            }
            SelectedUsedApplication.PersonId = null;
            if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                SelectedUsedApplication.PersonId = ((MstPersonEntity)txtPerson.Tag).PersonId;
            }
            SelectedUsedApplication.FromDate = Convert.ToDateTime("1900-01-01");
            if (dtpFromDate.EditValue!=null)
            {
                SelectedUsedApplication.FromDate = Convert.ToDateTime(dtpFromDate.EditValue);
            }
            SelectedUsedApplication.EndDate = Convert.ToDateTime("1900-01-01");
            if (dtpEndDate.EditValue!=null)
            {
                SelectedUsedApplication.EndDate =Convert.ToDateTime(dtpEndDate.EditValue);
            }

            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                SelectedUsedApplication.Status = "FUA01";
            }
        }
        private bool CheckData()
        {
            if (dtpApplicationDate.EditValue==null)
            {
                MessageBox.Show("申请日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpFromDate.EditValue == null)
            {
                MessageBox.Show("使用期限不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtApplicationPerson.Text))
            {
                MessageBox.Show("申请人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            //if (string.IsNullOrEmpty(txtPerson.Text))
            //{
            //    MessageBox.Show("所属人不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return false;
            //}
            if (cmbUsedType.SelectedValue == null || cmbUsedType.SelectedValue.ToString().Trim() == "")
            {
                MessageBox.Show("使用类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtReason.Text))
            {
                MessageBox.Show("领取物品不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FaUsedApplicationBs.SaveApplication(true, SelectedUsedApplication);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = FaUsedApplicationBs.SaveApplication(false, SelectedUsedApplication);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Approve)
                {
                    SetEntity();
                    SelectedUsedApplication.ApprovedDate = DateTime.Now;
                    SelectedUsedApplication.ApprovedPerson = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = FaUsedApplicationBs.ApproveUseApplication(SelectedUsedApplication);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.CEOApprove)
                {
                    if (txtAsset.Tag == null || string.IsNullOrEmpty(txtAsset.Text))
                    {
                        MessageBox.Show("请先选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                    SelectedUsedApplication.AssetId = ((FaAssetsEntity)txtAsset.Tag).AssetId;
                    SelectedUsedApplication.OperatedDate = DateTime.Now;
                    SelectedUsedApplication.OperatedBy = CommonParameter.CurrentUser.PersonId;
                    bool isSuccessful = FaUsedApplicationBs.ApproveUseApplication(SelectedUsedApplication);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("发货失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Events
        private void frmMaintainAssetUseApplication_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetFields();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            if (ThisActionType == ActionType.CEOApprove)
            {
                btnReject.Visible = false;
                btnApprove.Text = "发货";
            }
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_SaveNew()
        {
            if (Save())
            {
                ThisActionType = ActionType.New;
                SelectedUsedApplication = new FaUsedApplicationEntity();
                SelectedUsedApplication.ApplicationId = Guid.NewGuid().ToString();
                SetFields();
            }
        }
        protected override void Click_Approve()
        {
            if (SelectedUsedApplication == null)
            {
                return;
            }
            if (ThisActionType == ActionType.Approve)
            {
                SelectedUsedApplication.Status = "FUA02";
            }
            else if (ThisActionType == ActionType.CEOApprove)
            {
                SelectedUsedApplication.Status = "FUA04";
            }
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Reject()
        {
            if (SelectedUsedApplication == null)
            {
                return;
            }
            SelectedUsedApplication.Status = "FUA03";
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn + " (" + frm.SelectedPerson.PersonCode + ")";
                txtApplicationPerson.Tag = frm.SelectedPerson;

                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn + " (" + frm.SelectedPerson.PersonCode + ")";


                MstDepartmentEntity dept = MstDepartmentBs.GetMstDepartmentByID(frm.SelectedPerson.DepartmentId);
                if (dept != null)
                {
                    txtDepartment.Tag = dept;
                    txtDepartment.Text = dept.DepartmentName + " (" + dept.DepartmentCode + ")";
                }

            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }

        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                frm = new frmMaintainEmployeeQuery((MstDepartmentEntity)txtDepartment.Tag, true);
            }
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Text = frm.SelectedPerson.NameCn + " (" + frm.SelectedPerson.PersonCode + ")";
                txtPerson.Tag = frm.SelectedPerson;

                MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(frm.SelectedPerson.DepartmentId);
                if (department != null)
                {
                    txtDepartment.Text = department.DepartmentName + " (" + department.DepartmentCode + ")";
                    txtDepartment.Tag = department;
                }
            }
            else
            {
                txtPerson.Text = "";
                txtPerson.Tag = null;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
               frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
               if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
               {
                   txtDepartment.Text = frm.SelectedDepartment.DepartmentName + " (" + frm.SelectedDepartment.DepartmentCode + ")";
                   txtDepartment.Tag = frm.SelectedDepartment;
               }
               else
               {
                   txtDepartment.Text = "";
                   txtDepartment.Tag = null;
               }
        }

        private void txtAsset_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetQuery frm = new frmMaintainAssetQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAsset != null)
            {
                txtAsset.Text = frm.SelectedAsset.AssetCode + " (" + frm.SelectedAsset.AssetName + ")";
                txtAsset.Tag = frm.SelectedAsset;
            }
            else
            {
                txtAsset.Text = "";
                txtAsset.Tag = null;
            }
        }

        #endregion




    }
}